<?php
/**
 * Created by PhpStorm.
 * User: zcq
 * Date: 2019/4/2
 * Time: 15:51
 */

namespace app\admin\controller;


use think\Db;

class Difficulty extends Base
{
    public $table = 'difficulty';
    public function index()
    {
        $data = Db::name($this->table)->order('id desc')->select();
        $this->assign('data',$data);
        return $this->fetch();
    }

    public function ajax_add()
    {
        $data = [
            'name'=>input('name'),
            'sort'=>input('sort/d')?:0,
            'display'=>input('display/d'),
            'modtime'=>time(),
        ];

        $data['addtime'] = time();
        $a = Db::name($this->table)->insert($data);

        if($a){
            echo api_return(1,'保存成功');
        }else{
            echo api_return(0,'保存失败');
        }
        return;
    }

    public function ajax_update()
    {
        $field = input('field');
        $value = input('value');
        $data[$field] =$value;
        if($value == ''){
            $this->error('不能为空');
        }
        $id = input('id/d');
        $data['modtime'] = time();

        $a = Db::name($this->table)->where('id',$id)->update($data);
        if($a){
            echo api_return(1,'保存成功');
        }else{
            echo api_return(0,'保存失败');
        }
    }
}